Ext.define( "component.room.switch", {
	extend : 'Ext.panel.Panel',
	config : {
		treeId : ''
	},
	initComponent : function() {
		var me = this;
		var treeId = this.treeId;
		var houseTypeStore = new Ext.data.Store(
				{
					fields : [ "id", "houseTypeName" ],
					proxy : {
						type : 'ajax',
						url : contextPath
								+ '/estate/projectRoom/findProjectHouseType?roomQuery='
								+ treeId,
						reader : {
							type : 'json',
							root : 'root' // 数据
						}
					},
					autoLoad : false
				});
		houseTypeStore.load();
		var fs = new Ext.form.FieldSet({
			title : '查询条件',
			collapsible : false,// 可折叠

			defaults : {
				border : false, // 去掉panel的边框
				anchor : '100%'
			},
			fieldDefaults : {
				margin : 2,
				labelWidth : 80,
				labelAlign : "right",
				margin : "5 0 0 0"
			},
			items : [ {
				xtype : "container",
				layout : "hbox",
				width : 600,
				items : [ {
					xtype : 'combobox',
					id : 'houseTypeId',
					store : houseTypeStore,
					fieldLabel : '户型',
					displayField : "houseTypeName",
					valueField : "id",
					queryMode : 'local',
					typeAhead : true
				}, {
					xtype : "textfield",
					fieldLabel : "关键字",
					id : "modelRoomQuery"
				} ]
			}, {
				xtype : 'container',
				layout : "hbox",
				width : 600,
				items : [ {
					xtype : "checkboxgroup",
					fieldLabel : "房源状态",
					id : "modelRoomStatus",
					width : 600,
					items : [ {
						boxLabel : '可售',
						inputValue : '1'
					}, {
						boxLabel : '保留',
						inputValue : '2'
					}, {
						boxLabel : '认筹',
						inputValue : '3'
					}, {
						boxLabel : '认购',
						inputValue : '4'
					}, {
						boxLabel : '签约',
						inputValue : '5'
					}, {
						boxLabel : '销控',
						inputValue : '6'
					}, {
						boxLabel : '暂不可售',
						inputValue : '7'
					} ]

				} ]
			}, {
				xtype : 'panel',
				id : 'proportionFooterLable',
				frame : true,
				collapsible : true,
				autoScroll : true,
				header : false,
				width : 300,
				height : 50

			} ]
		});

					createPanleInfo(treeId);
						var roomSwitchColumns = [
								{
									text : "操作",
									dataIndex : 'id',
									locked   : true,
									renderer : function(value, cellmeta,
											record, rowIndex, columnIndex,
											store) {
										var roomStatus = record.data.roomStatus;
										var roomId = record.data.id;
										var projectId = record.data.projectId;
										var html = "";
										var addRetainUrl = "/marketing/retain/addRetainPage?roomId="
												+ roomId
												+ "&projectId="
												+ projectId + "&jumpFlag=0";
										var payDepositUrl = "/customer/payDeposit/toAdd?roomId="
												+ roomId
												+ "&projectId="
												+ projectId + "&jumpFlag=0";
										var subUrl = "/customer/subscribe/toAdd?roomId="
												+ roomId
												+ "&projectId="
												+ projectId + "&jumpFlag=0";
										var signedUrl = "/customer/signed/toAdd?roomId="
												+ roomId
												+ "&projectId="
												+ projectId + "&jumpFlag=0";
										var sellControlUrl = "/marketing/sellControl/addSellControlView?roomId="
												+ roomId
												+ "&projectId="
												+ projectId + "&jumpFlag=0";
										if (roomStatus == "1") {
											// 可售,应显示全部按钮
											// 判断是否具备功能权限进行展示
											if (!maketingRetainAdd) {
												// 保留
												var tabKey = "retein_" + roomId;
												html += getHtml(addRetainUrl,
														'保留', roomId, tabKey);
											}
											/*if (!marketingPayDepositAdd) {
												html += getHtml(payDepositUrl,
														'认筹', roomId, tabKey);
											}*/
											if (!marketingSubscribeAdd) {
												var tabKey = "subKey_" + roomId;
												html += getHtml(subUrl, '认购',
														roomId, tabKey);
											}
											if (!marketingSignedAdd) {
												var tabKey = "addSigned";
												html += getHtml(signedUrl,
														'签约', roomId, tabKey);
											}
											if (!marketingSellControlAdd) {
												var tabKey = "sell_" + roomId;
												html += getHtml(sellControlUrl,
														'销控', roomId, tabKey);
											}

										} else if (roomStatus == "3") {
											/*if (!marketingPayDepositAdd) {
												html += getHtml(payDepositUrl,
														'认筹', roomId, "");
											}*/
											if (!marketingSubscribeAdd) {
												var tabKey = "subKey_" + roomId;
												html += getHtml(subUrl, '认购',
														roomId, tabKey);
											}
											if (!marketingSignedAdd) {
												var tabKey = "addSigned";
												html += getHtml(signedUrl,
														'签约', roomId, tabKey);
											}
										} else if (roomStatus == "4") {
											if (!marketingSignedAdd) {
												html += clickMsg();
											}
										}
										return html;
									},
									width : 200
								},
								{
									text : '房号',
									dataIndex : 'combinedName',
									width : 350
								/*
								 * renderer:
								 * function(value,cellmeta,record,rowIndex,columnIndex,store) {
								 *
								 * var rec =
								 * roomSwitchGrid.getStore().getAt(rowIndex);
								 * var roomId = rec.get('id'); if(roomId!=null){
								 * return "<a style='text-decoration:none;'
								 * href='javascript:openWindow("+roomId+")'>"+value+"</a>"; }
								 * return value; }
								 */
								},
								/*{
									text : '公安编号',
									dataIndex : 'roomSecName',
                                    width : 350
								},*/
								{
									text : '房态',
									dataIndex : 'cStatus'
								},
								{
									text : '户型',
									dataIndex : 'houseTypeName'
								},
								{
									text : '建筑面积(平方米)',
									dataIndex : 'buildingArea'
								},
								{
									text : '分摊面积(平方米)',
									dataIndex : 'sharingArea'
								},
								{
									text : '套内面积(平方米)',
									dataIndex : 'innerArea'
								},
								{
									text : '计价面积(平方米)',
									dataIndex : 'valuationType',
									renderer : function(value, cellmeta,
											record, rowIndex, columnIndex,
											store) {
										var rec = roomSwitchGrid.getStore()
												.getAt(rowIndex);
										if (rec.get('valuationType') == 1) {
											var open = rec.get('buildingArea');
											return open;
										} else if (rec.get('valuationType') == 2) {
											var open = rec.get('innerArea');
											return open;
										}
										return value;
									}
								},
								{
									text : '单价(元)',
									dataIndex : 'danjia',
									renderer : Ext.util.Format
											.numberRenderer('0,000.00')
								},
								{
									text : "总价(元)",
									dataIndex : 'zongjia',
									renderer : Ext.util.Format
											.numberRenderer('0,000.00')
								} ];

						var roomSwitchGridStore = Ext
								.create(
										'Ext.data.Store',
										{
											fields : [ "id", "combinedName", "roomSecName",
													"houseTypeName",
													"buildingArea",
													"sharingArea", "innerArea",
													"billingArea",
													"buildingPrice",
													"totalAmount", "day",
													"danjia", "zongjia",
													"valuationType", "cStatus",
													"roomStatus", "projectId" ],
											pageSize : 20,
											remoteSort : false,
											proxy : {
												type : 'ajax',
												url : contextPath
														+ '/estate/projectRoom/modelList',
												reader : {
													type : 'json',
													root : 'rows',
													totalProperty : 'total'
												},
												actionMethods : {
													read : 'POST'
												}
											},
											listeners : {
												"beforeload" : function(store,
														op, options) {
													var parmas = {};
													var modelRoomStatus = Ext
															.getCmp(
																	'modelRoomStatus')
															.getChecked();
													var modelRoomStatusValue = "";
													Ext.Array
															.each(
																	modelRoomStatus,
																	function(
																			item) {
																		modelRoomStatusValue += item.inputValue
																				+ ',';
																	});
													var modelRoomQuery = Ext
															.getCmp(
																	"modelRoomQuery")
															.getValue();
													var houseTypeId = Ext
															.getCmp(
																	"houseTypeId")
															.getValue();
													parmas.houseTypeId = houseTypeId;
													parmas.modelRoomQuery = modelRoomQuery;
													parmas.modelRoomStatus = modelRoomStatusValue;
													parmas.roomQuery = treeId;
													Ext
															.apply(
																	store.proxy.extraParams,
																	parmas);
												}
											},
											autoLoad : true
										});

						var roomSwitchGrid = Ext
								.create(
										'Ext.grid.Panel',
										{
											minHeight : 600,
											title : '可售列表',
											columnLines : true,
											selModel : new Ext.selection.CheckboxModel(
													{
														checkOnly : true
													}),
											columns : roomSwitchColumns,
											store : roomSwitchGridStore,
											dockedItems : [ {
												xtype : 'pagingtoolbar',
												store : roomSwitchGridStore,
												dock : 'bottom',
												displayInfo : true,
												emptyMsg : "没有数据",
												displayMsg : "显示从{0}条数据到{1}条数据，共{2}条数据"
											} ],
											tbar : [{
												text : '切换回视图',
												icon : jypath
														+ '/resources/ExtJSicons/control/control_rewind_blue.png',
												handler : function() {
													Ext
															.getCmp(
																	"centerBody")
															.removeAll();
													var roomSwitchModel = Ext
															.create(
																	'component.room.switchTab',
																	{
																		treeId : treeId
																	});
													Ext
															.getCmp(
																	"centerBody")
															.add(
																	roomSwitchModel);
													$("#tab_type").val(
															1);
												}
											},
											{
												text : '批量销控',
												icon : jypath
														+ '/resources/ExtJSicons/flag/flag_pink.png',
												handler : function() {
													var records = roomSwitchGrid
															.getSelectionModel()
															.getSelection();
													var validCount = 0;
													var roomIds = "";
													if (null == records
															|| records == 0) {
														Ext.Msg
																.alert(
																		'提示',
																		'请选择销控房间!');
													} else {
														for (var i = 0; i < records.length; i++) {
															if (records[i].data.roomStatus == "1") {
																if (roomIds) {
																	roomIds += ",";
																}
																roomIds += records[i].data.id;
																validCount++;
															}
														}
														roomIds = roomIds
																.replace(
																		new RegExp(
																				/ROOM_/g),
																		"");
														if (roomIds) {
															var msg = "选中【"
																	+ records.length
																	+ "】条房源<br/>有效可售房源【"
																	+ validCount
																	+ "】条<br/>是否确认进行销控?"
															Ext.MessageBox
																	.confirm(
																			"系统提示",
																			msg,
																			function(
																					btn) {
																				if (btn == "yes") {
																					var windowInfo = Ext
																							.create(
																									'component.batch.sellControlWindow',
																									{
																										roomIds : roomIds,
																										userName : userName,
																										treeId : treeId
																									});
																					windowInfo
																							.show();
																				}
																			});
														} else {
															Ext.Msg
																	.alert(
																			'提示',
																			'请选择可售房源进行销控!');
														}

													}
												}
											},
											{
												text : '批量撤控',
												icon : jypath
														+ '/resources/ExtJSicons/flag/flag_pink.png',
												handler : function() {
													var records = roomSwitchGrid
															.getSelectionModel()
															.getSelection();
													if (null == records
															|| records == 0) {
														Ext.Msg
																.alert(
																		'提示',
																		'请选择撤控房间!');
													} else {
														var validCount = 0;
														var roomIds = "";
														for (var i = 0; i < records.length; i++) {
															if (records[i].data.roomStatus == "6") {
																if (roomIds) {
																	roomIds += ",";
																}
																roomIds += records[i].data.id;
																validCount++;
															}
														}
														roomIds = roomIds
																.replace(
																		new RegExp(
																				/ROOM_/g),
																		"");
														if (roomIds) {
															var msg = "选中【"
																	+ records.length
																	+ "】条房源<br/>有效可售房源【"
																	+ validCount
																	+ "】条<br/>是否确认进行撤控?"
															Ext.MessageBox
																	.confirm(
																			"系统提示",
																			msg,
																			function(
																					btn) {
																				if (btn == "yes") {
																					var windowInfo = Ext
																							.create(
																									'component.batch.sellCancelControlWindow',
																									{
																										roomIds : roomIds,
																										userName : userName,
																										treeId : treeId
																									});
																					windowInfo
																							.show();
																				}
																			});
														} else {
															Ext.Msg
																	.alert(
																			'提示',
																			'房源状态需为销控!');
														}
													}
												}
											},
											/*{
												xtype : "button",
												text : "公安编号",
												text : "公安编号",
												icon : jypath
												+ '/resources/ExtJSicons/flag/flag_pink.png',
												handler : function() {
													if (!treeId) {
														Ext.MessageBox.alert("系统提示", "请选择项目");
														return;
													}
													var fromPanel = Ext .create( "Ext.form.Panel", {
														collapsible : false,
														autoScroll : false,
														defaultType : 'textfield',
														defaults : {
															anchor : '100%'
														},
														fieldDefaults : {
															flex : 1
														},
														items : [ {
															xtype : 'filefield',
															fieldLabel : '导入excel',
															emptyText : '请上传excel文件',
															id : 'multipartFile',
															name : 'multipartFile',
															msgTarget : 'side',
															allowBlank : false,
															anchor : '95%',
															buttonConfig : {
																iconCls : 'silk-folder'
															},
															buttonText : '选择文件'
														} ]
													});

													var window = Ext.create('Ext.Window', {
														title : "批量导入",
														width : 300,
														height : 150,
														closable : true,
														modal : true,
														headerPosition : 'top',
														items : [ fromPanel ],
														buttons : [{
															xtype : "button",
															text : "开始导入",
															iconCls : 'icon-save',
															handler : function() {
																var form = fromPanel.getForm();
																if (!treeId) {
																	Ext.MessageBox.alert("系统提示", "请选择项目");
																	return;
																}
																if (form.isValid()) {
																	var params = form.getValues();
																	params.treeId = treeId;
																	form.submit({
																		timeout : 7200,
																		url : contextPath
																		+ '/estate/excel/importModel',
																		params : params,
																		method : "POST",
																		waitMsg : '正在上传...',
																		success : function(
																			form,
																			action) {
																			Ext.MessageBox.alert("系统提示","导入成功");
																		},
																		failure : function(
																			form,
																			action) {
																			var obj = JSON.parse(action.response.responseText);
																			var result = obj.obj;
																			Ext.MessageBox.alert("提示", result, function (but) {
																				window.close();
																				roomSwitchGrid.getStore().reload();
																			});
																		}
																	});
																}
															}
														},
														{
															xtype : "button",
															text : "模板下载",
															iconCls : 'icon-save',
															handler : function() {
																if(!treeId){
																	Ext.Msg.alert('提示', '请选择左侧菜单树中的项目!');
																	return;
																}
																location.href=jypath+"/estate/excel/downloadRoomsInfo?treeId="+treeId;
															}
														}]
													});
													window.show();
												}
											},*/
											{
												text : '查询',
												icon : jypath
														+ '/resources/ExtJSicons/arrow/arrow_refresh.png',
												handler : function() {
													roomSwitchGrid
															.getStore()
															.reload();
												}
											} ]
										});

						var defaultConfig = {
							defaults : {
								split : false, //有分割线
								collapsible : false, //可以折叠
								forceFit : false
							},
							items : [ fs, roomSwitchGrid ]
						};
						Ext.apply(this, defaultConfig);
						this.callParent();
					}
				});

function openWindow(roomId) {
	//弹出详情框
	var windowInfo = Ext.create('component.room.InfoWindow', {
		roomId : roomId,
		jumpFlag : '1'
	});
	windowInfo.show();
}
function getHtml(url, title, roomId, tabKey) {
	var html = "<a style='text-decoration: none;margin-left: 10px;' href='javascript:parent.openMenu(\"0\",\""
			+ tabKey
			+ "\",\"\",\""
			+ title
			+ "\",\""
			+ url
			+ "\")'>"
			+ title
			+ "</a>";
	return html;
}

function clickMsg() {
	var html = "<a href='javaScript:msgHtml()' style='text-decoration: none;margin-left: 10px;'>签约</a>";
	return html;
}
function msgHtml() {
	Ext.Msg.alert('提示', '房源已被认购,如需转签约,请前往"认购记录详情"页面进行操作');
}